package Search;

public class Ceil {

    private Ceil(){}
    //查找比目标小的最大数，没的话返回大于目标最小数
    public static <E extends Comparable<E>> int ceil(E[] data,E target){
        int c = upper(data,target);
        if (c > 0 && data[c - 1].compareTo(target) == 0)
            return c - 1;
        return c;
    }

    private static <E extends Comparable<E>> int upper(E[] data,E target){
        int l = 0,r = data.length;
        while (l < r){
            int mid = l + (r - l) / 2;
            if (data[mid].compareTo(target) <= 0)
                l = mid + 1;
            else
                r = mid;
        }
        return l;
    }
}
